﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class ScreenManager : GameScreen
    {
        public List<ScreenInfo> Screens;
        private int actualScreen;
        public int ActualScreen
        {
            get { return actualScreen; }
            set
            {
                if (actualScreen != value)
                {
                    lastScreen = actualScreen;
                }
                actualScreen = value;
                if (Screens.Count > actualScreen && actualScreen != -1)
                {                    
                    if (Screens[actualScreen].Screen is MenuScreen)
                    {
                        MenuScreen scr = Screens[actualScreen].Screen as MenuScreen;
                        scr.CanSelect = true;
                    }

                    if (Screens.Count > lastScreen && lastScreen != -1)
                    {
                        if (Screens[lastScreen].Screen is MenuScreen)
                        {
                            MenuScreen scr = Screens[lastScreen].Screen as MenuScreen;
                            scr.CanSelect = false;
                        }
                        Screens[lastScreen].Screen.VisibilityChange(false);
                    }
                    Screens[actualScreen].Screen.VisibilityChange(true);
                }                
            }
        }
        private int lastScreen;
        public IScreenTransition TransitionToUse;        

        public ScreenManager()
        {
            Screens = new List<ScreenInfo>();
        }

        public int IndexOfScreen(GameScreen gs)
        {
            for (int i = 0; i < Screens.Count; i++)
            {
                if (Screens[i].Screen == gs)
                    return i;
            }
            return -1;
        }
        public int IndexOfScreen(Type screenType)
        {
            for (int i = 0; i < Screens.Count; i++)
            {
                if (Screens[i].Screen.GetType() == screenType)
                    return i;
            }
            return -1;
        }

        public void ChangeScreen(Type ScreenType, IScreenTransition Transition)
        {
            ChangeScreen(IndexOfScreen(ScreenType), Transition);
        }
        public void ChangeScreen(GameScreen gScreen, IScreenTransition Transition)
        {
            ChangeScreen(IndexOfScreen(gScreen), Transition);
        }
        public void ChangeScreen(int ScreenIndex, IScreenTransition Transition)
        {
            if (ScreenIndex != -1)
            {
                this.TransitionToUse = Transition;
                this.ActualScreen = ScreenIndex;
            }
        }

        public ScreenInfo Add(GameScreen screen)
        {
            ScreenInfo info = new ScreenInfo(screen, null, null);
            this.Screens.Add(info);
            return info;
        }

        public override void Update()
        {
            base.Update();

            if (Screens.Count > ActualScreen && ActualScreen != -1)
            {
                Screens[ActualScreen].Screen.Update();
                if (TransitionToUse != null &&
                    !TransitionToUse.Finished)
                {
                    TransitionToUse.Update();
                    if (lastScreen != -1)
                    {
                        Screens[lastScreen].Screen.Update();
                    }
                }

                if (Screens[ActualScreen].BackScreen != null)
                {
                    if (Engine.Input.IsActivePlayerButtonPress(Buttons.B) ||
                        Engine.Input.IsKeyboardPress(Keys.Escape))
                    {
                        for (int i = 0; i < Screens.Count; i++)
                        {
                            if (Screens[i].Screen == Screens[ActualScreen].BackScreen)
                            {
                                ActualScreen = i;
                                TransitionToUse = Screens[i].BackTransition;
                            }
                        }
                    }
                }
            }            
        }

        public Texture2D CurrentTex;
        public Texture2D NextTex;        
        public override void Draw()
        {
            base.Draw();

            if (Screens.Count > ActualScreen && ActualScreen != -1)
            {
                Engine.QueuedActions.AddStartQueuedAction(0, CalculateCurrent);
                Vector2 lastPos;
                Vector2 nexPos;                
                if (TransitionToUse is IScreenPositionTransition)
                {
                    IScreenPositionTransition transition = TransitionToUse as IScreenPositionTransition;
                    lastPos = transition.LastScreenPosition;
                    nexPos = transition.NextScreenPosition;
                }
                else
                {
                    lastPos = Vector2.Zero;
                    nexPos = Vector2.Zero;
                }

                Screens[ActualScreen].Screen.Position = nexPos;
                
                if (CurrentTex != null)
                    Engine.RenderManager.Draw(CurrentTex, nexPos, null, false);

                if (TransitionToUse != null &&
                    !TransitionToUse.Finished &&
                    NextTex != null)
                {
                    if (lastScreen != -1)
                    {
                        Screens[lastScreen].Screen.Position = lastPos;
                    }
                    TransitionToUse.Draw();
                    Engine.RenderManager.Draw(NextTex, lastPos, null, false);
                }
            }
        }
        private void CalculateCurrent()
        {            
            CurrentTex = Screens[ActualScreen].Screen.RenderScreen();

            if (TransitionToUse != null &&
                    !TransitionToUse.Finished)
            {
                if (lastScreen != -1)
                {
                    NextTex = Screens[lastScreen].Screen.RenderScreen();
                }
            }
        }
    }
}
